import {isNumber} from 'lodash';

const PAGE_SIZE_OPTIONS = ['10', '20', '50', '100'];

/**
 * author: mengyao.xi
 * 获取URL参数
 */
export function getQueryString() {
  let result = {url: ''}
  let query
  const url = window.location.hash.split('?')
  // console.log('url', url)
  if (url.length > 1) {
    if (url.length > 2) {
      query = url[1] + '?' + url[2];
    } else if (url.length == 2) {
      query = url[1]
    }
    // console.log('query', url, query, query.split('url=')[1])
    result = {url: query.split('url=')[1]}
  }
  //   if (query) {
  //   const queryArr = query.split('&')
  //   queryArr.forEach(item => {
  //   const value = item.split('=')[1]  
  //   const key = item.split('=')[0]
  //   const results = result
  //   results [key] = value
  //   })
  // }
  // console.log('result', result)
  return result  
}

/**
 * 渲染分页数据中的信息
 * @param {number} total - 数据总数
 * @param {number[]} range - [当前数据的第一条数据序号, 当前数据最后一条数据序号]
 */
function totalRender(total, range) {
  return `显示 ${range[0]} - ${range[1]} 共 ${total} 条`;
}

/**
 * 根据传入的数据列表对象，生成页面分页参数对象
 * @param {object} data - 数据列表对象
 * @returns {object} pagination- 分页对象
 */
export function createPagination(data) {
  if (data) {
    return {
      showSizeChanger: true,
      pageSizeOptions: [...PAGE_SIZE_OPTIONS],
      // showQuickJumper: true,
      current: isNumber(data.number) ? data.number : data.start,
      pageSize: data.size, // 每页大小
      total: isNumber(data.totalElements) ? data.totalElements : data.total,
      showTotal: totalRender,
    };
  }
}

export const formatData = (res, page_) => {
  return {
    data: res?.healthAlertsVoList.map((item, i) => ({
      ...item,
      id: `assetId${i}100`,
    })),
    tP: {
      ...page_,
      pagination: createPagination({
        number: page_.pagination?.current || 1,
        size: page_.pagination?.pageSize || 10,
        total: res?.total,
      }),
    },
  };
};
export const getDefaultData = () => {
  return {
    data: [],
    tP: {
      pagination: {
        current: 1,
        pageSize: 10,
      },
    },
  };
};
